package orm

import (
	"fmt"
	"gamesdk/common/log"
	"time"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var DB *gorm.DB

//Init db
func ConnectUserDb(user, pwd, ip, database string) {
	var err error
	uri := fmt.Sprintf(`%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local`, user, pwd, ip, database)
	fmt.Println("数据库链接：：：", uri)
	DB, err = gorm.Open("mysql", uri)
	if err != nil {
		panic("connect to mysql error:" + err.Error())
		return
	}
	DB.DB().SetMaxIdleConns(200)
	DB.DB().SetMaxOpenConns(200)
	DB.DB().SetConnMaxLifetime(20 * time.Second)

	DB.LogMode(true)
	log.Debug("connect to logDb mysql ...")

	DB.SingularTable(true)
}

func GetDb() *gorm.DB {
	return DB
}
